package com.njtoyo.taxi.admin.service.local_service.merchant.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njtoyo.taxi.admin.library.common.OperationRecordHelper;
import com.njtoyo.taxi.admin.mapper.master.entity.local_service.GeoMerchantRegion;
import com.njtoyo.taxi.admin.mapper.master.entity.local_service.LifeMerchant;
import com.njtoyo.taxi.admin.mapper.master.mapper.local_service.GeoMerchantRegionMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.local_service.LifeMerchantMapper;
import com.njtoyo.taxi.admin.rest.presenter.local_service.merchant.LifeMerchantExaminePresenter;
import com.njtoyo.taxi.admin.rest.wrapper.local_service.merchant.examine.ExamineWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.local_service.merchant.examine.LifeMerchantExamineWrapper;
import com.njtoyo.taxi.admin.service.local_service.merchant.MerchantExamineService;
import com.taxi.entity.common.RestResult;
import com.taxi.entity.common.ResultEnum;
import com.njtoyo.taxi.entity.backend.AdminUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.hutool.core.bean.BeanUtil.copyProperties;
import static cn.hutool.core.util.ObjectUtil.isEmpty;
import static cn.hutool.core.util.ObjectUtil.isNotEmpty;
import static java.util.stream.Collectors.toList;

@Slf4j
@Service
public class MerchantExamineServiceImpl implements MerchantExamineService {
    @Autowired
    private OperationRecordHelper operationRecordHelper;

    @Autowired
    private LifeMerchantMapper lifeMerchantMapper;

    @Autowired
    private GeoMerchantRegionMapper merchantRegionMapper;

    /**
     * 商户审核列表
     *
     * @param adminUser
     * @param wrapper
     * @return
     */
    @Override
    public RestResult<Page<LifeMerchantExaminePresenter>> getExaminePage(AdminUser adminUser, LifeMerchantExamineWrapper wrapper) {
        switch (adminUser.getAccountType()) {
            case platform:
                break;
            default:
                return RestResult.failed("账号权限不够");
        }
        Page<LifeMerchantExaminePresenter> page = lifeMerchantMapper.getExaminePage(wrapper);

        List<LifeMerchantExaminePresenter> list = page.getRecords();
        if (isNotEmpty(list)) {
            List<Long> merchantRegionIds = list.stream()
                    .map(LifeMerchantExaminePresenter::getMerchantRegionId).collect(toList());

            //地区名称
            QueryWrapper<GeoMerchantRegion> merchantRegionWrapper = new QueryWrapper<>();
            merchantRegionWrapper.select("id", "name");
            merchantRegionWrapper.in("id", merchantRegionIds);
            List<Map<String, Object>> maps = merchantRegionMapper.selectMaps(merchantRegionWrapper);
            Map<Long, String> map = maps.stream().collect(Collectors.toMap(i -> Long.valueOf(i.get("id").toString()), i -> i.get("name").toString()));

            list.forEach(lifeMerchantExaminePresenter -> {
                lifeMerchantExaminePresenter.setMerchantRegionName(map.getOrDefault(lifeMerchantExaminePresenter.getMerchantRegionId(), null));
            });
        }
        return RestResult.success(page);
    }

    /**
     * 商户审核详情
     *
     * @param id
     * @return
     */
    @Override
    public RestResult<LifeMerchantExaminePresenter> details(Long id) {
        LifeMerchant lifeMerchant = lifeMerchantMapper.selectById(id);
        if (isEmpty(lifeMerchant)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }
        LifeMerchantExaminePresenter examinePresenter = copyProperties(lifeMerchant, LifeMerchantExaminePresenter.class);
        if (isNotEmpty(examinePresenter)) {
            //地区名称
            QueryWrapper<GeoMerchantRegion> merchantRegionWrapper = new QueryWrapper<>();
            merchantRegionWrapper.select("id", "name");
            merchantRegionWrapper.eq("id", examinePresenter.getMerchantRegionId());
            GeoMerchantRegion geoMerchantRegion = merchantRegionMapper.selectOne(merchantRegionWrapper);
            if (isNotEmpty(geoMerchantRegion)) {
                examinePresenter.setMerchantRegionName(geoMerchantRegion.getName());
            }
        }
        return RestResult.success(examinePresenter);
    }

    /**
     * 商户审核
     *
     * @param id
     * @param wrapper
     * @return
     */
    @Override
    public RestResult examine(Long id, ExamineWrapper wrapper) {
        LifeMerchant lifeMerchant = lifeMerchantMapper.selectById(id);
        if (isEmpty(lifeMerchant)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }
        //商户
        new LambdaUpdateChainWrapper<>(lifeMerchantMapper)
                .eq(LifeMerchant::getId, id)
                .set(LifeMerchant::getState, wrapper.getState())
                .set(LifeMerchant::getUpdatedAt, new Date())
                .update();
        operationRecordHelper.addBusinessLog(String.format("审核了商户 [%d] 的信息", id));
        return RestResult.success();
    }
}
